package com.uchedao.vehicledetection.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.model.Draft;

import java.util.ArrayList;

public class DraftAdapter extends BaseAdapter {
	public static final int ACTION_OPEN = 0;
	public static final int ACTION_EDIT = 1;
	public static final int ACTION_DELETE = 2;

	private ArrayList<Draft> list = new ArrayList<Draft>();

	public interface DraftListener {
		public void onItemClick(int action, int position);
	}

	private LayoutInflater mInflater;
	private DraftListener listener;

	public DraftAdapter(Context context) {
		mInflater = LayoutInflater.from(context);
	}

	public DraftAdapter(Context context, ArrayList<Draft> array) {
		mInflater = LayoutInflater.from(context);
		list = array;
	}

	public void setList(ArrayList<Draft> array) {
		list = array;
		notifyDataSetChanged();
	}

	public void setDraftListener(DraftListener draftListener) {
		listener = draftListener;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {
		TextView titleTv;
		TextView cardTv;
		TextView vinTv;
		TextView timeTv;
		Button deleteBtn;
		Button editBtn;
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (listener != null) {
				int position = (Integer) v.getTag(R.layout.item_drafts);
				listener.onItemClick(ACTION_OPEN, position);
			}
		}
	};

	private OnClickListener editListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (listener != null) {
				int position = (Integer) v.getTag();
				listener.onItemClick(ACTION_EDIT, position);
			}
		}
	};

	private OnClickListener deleteListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (listener != null) {
				int position = (Integer) v.getTag();
				listener.onItemClick(ACTION_DELETE, position);
			}
		}
	};

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_drafts, null);
			viewHolder.titleTv = (TextView) convertView.findViewById(R.id.item_draft_title_tv);
			viewHolder.cardTv = (TextView) convertView.findViewById(R.id.item_draft_cards_tv);
			viewHolder.vinTv = (TextView) convertView.findViewById(R.id.item_draft_vin_tv);
			viewHolder.timeTv = (TextView) convertView.findViewById(R.id.item_draft_public_time_tv);
			viewHolder.deleteBtn = (Button) convertView.findViewById(R.id.item_draft_delete_btn);
			viewHolder.deleteBtn.setTag(position);
			viewHolder.editBtn = (Button) convertView.findViewById(R.id.item_draft_edit_btn);
			viewHolder.editBtn.setTag(position);
			viewHolder.editBtn.setOnClickListener(editListener);
			viewHolder.deleteBtn.setOnClickListener(deleteListener);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Draft draft = list.get(position);
		viewHolder.titleTv.setText(getModel(draft));
		viewHolder.cardTv.setText(draft.car_plate);
		viewHolder.vinTv.setText(draft.car_vin);
		viewHolder.timeTv.setText(draft.create_time);
		return convertView;
	}

	private String getModel(Draft draft) {
		if (draft.base_info == null || draft.base_info.use_info == null || draft.base_info.use_info.brand_id == -1)
			return "品牌车系车型";
		else {
			String model = draft.base_info.use_info.brand_str;

			if (!TextUtils.isEmpty(draft.base_info.use_info.series_str)) {
				model += draft.base_info.use_info.series_str;
			}

			if (!TextUtils.isEmpty(draft.base_info.use_info.model_str)) {
				if (!"手动输入".equals(draft.base_info.use_info.model_str)) {
					model += draft.base_info.use_info.model_str;

				} else if (!TextUtils.isEmpty(draft.base_info.use_info.model_custom)) {
					model += draft.base_info.use_info.model_custom;
				}

			}
			return model;
		}
	}

	/**
	 * 得到当前完成项
	 * 
	 * @param draft
	 * @return
	 */
	private int getProgress(Draft draft) {
		int k = 0;
		for (int i : draft.status) {
			if (i == Draft.STATUE_FINISH)
				k++;
		}
		return k;
	}
}
